package com.google.devrel.example.android.protips.mvp;

import java.text.DecimalFormat;
import java.text.MessageFormat;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.view.MenuItem;

public interface IMainView {

	public static final DecimalFormat df = new DecimalFormat("###.##");

	public static final int MENUITEM_MAIN = Menu.FIRST;
	public static final int MENUITEM_TRACK = MENUITEM_MAIN + 1;
	public static final int MENUITEM_MONITOR = MENUITEM_MAIN + 3;
	public static final int MENUITEM_SERVICE = MENUITEM_MAIN + 4;
	public static final int MENUITEM_MISC = MENUITEM_MAIN + 5;

	public boolean buildMenu(Menu menu);

	public boolean handleMenuItemSelected(int featureId, MenuItem item);

	public void setOrientationText(String disp, String physic);

	class GyroOrientationSensorListener implements IOrientationSensorListener,
			SensorEventListener {

		private OrientationChangeListener listener;
		private SensorManager sensorManager;

		public GyroOrientationSensorListener(SensorManager sm) {
			sm.registerListener(this,
					sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE),
					SensorManager.SENSOR_DELAY_UI);
			this.sensorManager = sm;
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
				listener.onOrientationChanged(MessageFormat.format(
						"x:{0},y:{1},z:{2}", df.format(event.values[0]),
						df.format(event.values[1]), df.format(event.values[2])));
			}
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}

		@Override
		public void setOrientationChangeListener(
				OrientationChangeListener listener) {
			this.listener = listener;
		}

		@Override
		public void removeAllListeners() {
			if (this.sensorManager != null) {
				this.sensorManager.unregisterListener(this);
			}
		}

		@Override
		public String getType() {
			return "TYPE_GYROSCOPE";
		}
	}

	class AccOrientationSensorListener implements IOrientationSensorListener,
			SensorEventListener {

		private OrientationChangeListener listener;
		private SensorManager sensorManager;

		public AccOrientationSensorListener(SensorManager sm) {
			sm.registerListener(this,
					sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
					SensorManager.SENSOR_DELAY_UI);
			this.sensorManager = sm;
		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
				listener.onOrientationChanged(MessageFormat.format(
						"x:{0},y:{1},z:{2}", df.format(event.values[0]),
						df.format(event.values[1]), df.format(event.values[2])));
			}
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}

		@Override
		public void setOrientationChangeListener(
				OrientationChangeListener listener) {
			this.listener = listener;
		}

		@Override
		public void removeAllListeners() {
			if (this.sensorManager != null) {
				this.sensorManager.unregisterListener(this);
			}
		}

		@Override
		public String getType() {
			return "TYPE_ACCELEROMETER";
		}
	}
}

interface IOrientationSensorListener {
	public void setOrientationChangeListener(OrientationChangeListener listener);

	public void removeAllListeners();

	public String getType();
}

interface OrientationChangeListener {
	public void onOrientationChanged(String side);
}
